<template>
  <el-container>
    <el-header>
      <div class="title">
        <img src="@/assets/images/logo.jpg" alt="" width="40">
        <span>万云花店后台管理系统</span>
      </div>
      <div class="userInfo">
        <span>欢迎你,admin</span>
        <!-- <el-button type="primary" >注销</el-button> -->
        <input class="logoutBtn" type="button" value="注销" @click="handleLogout">
      </div>
    </el-header>
    <el-container>
      <el-aside :width="!isCollapse ? '200px' : '64px'">
        <!-- <el-aside width="200px"> -->

        <!-- 侧边导航折叠开关 -->
        <p class="menuCollapseBtn" @click="menuCollapseBtn">
          <i class="el-icon-s-unfold" v-if="isCollapse"></i>
          <i class="el-icon-s-fold" v-else></i>
        </p>

        <!-- 侧边导航栏 -->
        <el-menu ref="mainMenuRef" :default-active="defaultIndex" class="el-menu-vertical-demo" :unique-opened="true" :router="true"
          :collapse="isCollapse" background-color="#aaa" text-color="#fff">

          <!-- 一级菜单 -->
          <el-submenu :index="item.path" v-for="item in menusList" :key="item.id">
            <template slot="title">
              <i :class="iconList[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>

            <!-- 二级菜单额容器 -->
            <el-menu-item-group>

              <!-- 二级菜单 -->
              <el-menu-item :index="subItem.path" v-for="subItem in item.children" :key="subItem.id"
                @click="handleSubMenuSave(subItem.path)">
                <template slot="title">
                  <!-- 这里使用的iconfont中的图标 -->
                  <i class="iconfont icon-danju"></i>
                  <span>{{ subItem.authName }}</span>
                </template>
              </el-menu-item>

            </el-menu-item-group>
          </el-submenu>


        </el-menu>

      </el-aside>
      <el-main>
        <!-- 这里的router-view对应切换的配置项是router文件中/home下面的children -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>

export default {
  name: 'HomeView',
  data: function () {
    return {
      // 左侧权限菜单
      menusList: [],
      // 默认展开菜单的索引
      defaultIndex: '',
      // 菜单是否折叠
      isCollapse: false,
      // 一级列表图标
      iconList: {
        '125': 'el-icon-user-solid',
        '103': 'el-icon-s-cooperation',
        '101': 'el-icon-s-goods',
        '102': 'el-icon-s-shop',
        '145': 'el-icon-s-marketing',
      }
    }
  },
  components: {

  },
  methods: {
    // 注销登录
    handleLogout() {
      sessionStorage.removeItem('token')
      this.$message('注销登录成功!')
      this.$router.push('/login')
      sessionStorage.removeItem('curPath')
    },
    // 获取左侧导航栏的数据
    async getMenusList() {
      const { data, meta } = await this.$http.get('menus')
      // console.log(data)
      if (meta.status !== 200) {
        return this.$message({
          message: meta.msg,
          type: 'error'
        })
      }

      // 可以在这里处理data数据添加图标

      this.menusList = data

    },
    // 存储当前点击路径
    handleSubMenuSave(path) {
      this.defaultIndex = path
      sessionStorage.setItem('curPath', path)
    },
    // 菜单折叠开关事件
    menuCollapseBtn() {
      this.isCollapse = !this.isCollapse
    }
  },
  watch:{
    $route:{
      handler:function(val){     
        if(val.path === '/welcome'){
          // 去掉高亮
          this.defaultIndex = ''
          // 将menu指定二级菜单折叠
          console.log(this.$refs.mainMenuRef)
          this.$refs.mainMenuRef.close(sessionStorage.getItem('curPath'))
          // 删除缓存
          sessionStorage.removeItem('curPath')

        }
      },
      // immediate:true
    }
  },
  mounted() {
    this.getMenusList()

    // 每次进入页面，就要从本地存储中获取当前展开二级菜单path值
    this.defaultIndex = sessionStorage.getItem('curPath')

    console.log(this.defaultIndex);   // ''

  }
}
</script>

<style lang="less" scoped>
// 1.less中是支持变量
@fontWhite: white;
@mrVal: 10px;

.el-aside {
  transition: width .2s linear;
}

.el-menu {
  box-sizing: border-box;
}

.el-menu-item-group {
  background-color: #ddd;
  // padding-bottom: 14px;
}

// vue深层选择器
:deep(.el-menu-item-group__title) {
  padding: 0 !important;
}

.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  // min-height: 400px;
}

.menuCollapseBtn {
  width: 100%;
  line-height: 40px;
  text-align: center;
  cursor: pointer;
  font-size: 30px;
}

.el-header {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  // 引用变量
  color: @fontWhite;

  // 2. 支持选择器嵌套
  .title {
    display: flex;
    align-items: center;

    img {
      margin-right: @mrVal;
    }
  }

  div.userInfo {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .logoutBtn {
      padding: 7px 12px;
      border: none;
      border-radius: 3px;
      background-color: rgb(6, 165, 218);
      color: white;
      margin-left: @mrVal;
      cursor: pointer;
    }
  }

}

// .el-header .title{ }</style>
